package a.codetop;

import 其他.DataStructure.ListNode;

public class _20链表相加 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ListNode dmt = new ListNode(0);
        ListNode dm = dmt;
        int cr = 0;

        while(l1 != null || l2 != null || cr != 0) {
            int v1 = l1!=null ? l1.val : 0;
            int v2 = l2!=null ? l2.val : 0;

            int sum = v1 + v2 + cr;
            int shang = sum/10; cr=shang;
            int yushu = sum%10; dm.next = new ListNode(yushu); dm=dm.next;

            if(l1!=null)l1=l1.next; if(l2!=null)l2=l2.next;
        }

        return dmt.next;

    }
}
